<template>
    <div class="app-container">
        <!-- 搜索 -->
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
            <el-form-item label="套餐名" prop="title">
                <el-input v-model="queryParams.title" placeholder="请输入套餐名" clearable>
                </el-input>
            </el-form-item>
            <input hidden />
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>
        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button type="primary" plain icon="el-icon-plus" size="mini"
                    @click="handleAddProducts" v-hasPermi="['resource:package:add']">添加</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple"
                    @click="handleDelete" v-hasPermi="['resource:package:remove']">删除</el-button>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getDataList"></right-toolbar>
        </el-row>
        <!-- 表格 -->
        <el-table v-loading="loading" :data="dataList" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column label="ID" prop="id" width="60" />
            <el-table-column label="套餐名" prop="title" width="100" />
            <el-table-column label="充值金额" prop="amount" width="80" />
            <el-table-column label="到账金额" prop="amountGet" width="80" />
            <el-table-column label="抵扣比例" prop="ratio" width="80" />
            <el-table-column label="备注" prop="remark" width="200" show-overflow-tooltip />
            <el-table-column label="图文说明" prop="content" />
            <el-table-column label="操作" align="center" width="200" class-name="small-padding fixed-width" fixed="right">
                <template slot-scope="scope">
                    <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                        v-hasPermi="['resource:package:edit']">修改</el-button>
                    <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                        v-hasPermi="['resource:package:remove']">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <!-- 分页 -->
        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize" @pagination="getDataList" />
        <!-- 添加或编辑弹框 -->
        <el-dialog :title="addDialogTitle" :visible.sync="addDialogVisible" width="600px" append-to-body>
            <el-form ref="form" :model="form" :rules="rules" size="medium" label-width="100px">
                <el-row>
                    <el-form-item label="套餐名" prop="title">
                        <el-input v-model="form.title" placeholder="请输入套餐名" clearable>
                        </el-input>
                    </el-form-item>
                    <el-form-item label="充值金额" prop="amount">
                        <el-input type="number" v-model="form.amount" placeholder="请输入充值金额" clearable>
                        </el-input>
                    </el-form-item>
                    <el-form-item label="到账金额" prop="amountGet">
                        <el-input type="number" v-model="form.amountGet" placeholder="请输入到账金额" clearable>
                        </el-input>
                    </el-form-item>
                    <el-form-item label="抵扣比例" prop="ratio">
                        <el-input type="number" v-model="form.ratio" placeholder="请输入抵扣比例" clearable>
                            <template slot="append">%</template>
                        </el-input>
                    </el-form-item>
                    <el-form-item label="备注" prop="remark">
                        <el-input v-model="form.remark" placeholder="请输入备注" clearable>
                        </el-input>
                    </el-form-item>
                    <el-form-item label="图文说明" prop="content">
                        <editor v-model="form.content"></editor>
                    </el-form-item>
                </el-row>
            </el-form>
            <div slot="footer">
                <el-button @click="cancel">取消</el-button>
                <el-button type="primary" @click="submitForm">确定</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
// 快递平台管理
import { getList, getInfo, addList, editList, removeList } from '@/api/resource'
export default {
    name: 'CustomerList',
    dicts: ['sys_price_type', 'self_order_payment_type'],
    data() {
        return {
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                title: undefined, // 平台名字
                amount: undefined, // 平台编号
            },
            // 数据列表
            dataList: [],
            // 添加弹框标题
            addDialogTitle: '',
            // 添加弹框是否显示
            addDialogVisible: false,
            form: {},
            // 添加弹框表单校验
            rules: {
                title: [{ required: true, message: '请输入套餐名', trigger: 'change' }],
                amount: [{ required: true, message: '请输入充值金额', trigger: 'blur' }],
                amountGet: [{ required: true, message: '请输入到账金额', trigger: 'blur' }],
                ratio: [{ required: true, message: '请输入抵扣比例', trigger: 'blur' }],
                content: [{ required: true, message: '请输入图文说明', trigger: 'blur' }],
            },
            isStatus: false,

        }
    },
    created() {
        this.getDataList()
    },
    methods: {
        changeType(e) {
            this.form.accountType = e.value
        },
        /** 查询列表 */
        getDataList() {
            this.loading = true
            getList(this.queryParams).then((response) => {
                this.dataList = response.rows
                this.total = response.total
                this.loading = false
            })
        },
        // 取消按钮
        cancel() {
            this.addDialogVisible = false;
        },
        reset() {
            this.form = {};
            this.resetForm("form");
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1
            this.getDataList()
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm('queryForm')
            this.handleQuery()
        },
        /** 多选框选中数据 */
        handleSelectionChange(selection) {
            this.ids = selection.map((item) => item.id)
            this.single = selection.length != 1
            this.multiple = !selection.length
        },
        handleAddProducts() {
            this.reset();
            this.isStatus = false
            this.addDialogTitle = '添加套餐'
            this.addDialogVisible = true
        },
        handleUpdate(row) {
            this.reset();
            getInfo({ id: row.id }).then((response) => {
                this.form = response.data;
                this.isStatus = true;
                this.addDialogVisible = true;
                this.addDialogTitle = "修改套餐";
            });
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            let userIds = row.id || this.ids;
            if (typeof userIds == "number") {
                userIds = [userIds];
            }
            this.$modal
                .confirm('是否确认删除ID为"' + userIds + '"的数据项？')
                .then(function () {
                    return removeList({ ids: userIds });
                })
                .then(() => {
                    this.getDataList();
                    this.$modal.msgSuccess("删除成功");
                })
                .catch(() => { });
        },
        /** 提交按钮 */
        submitForm: function () {
            console.log(this.form);
            this.$refs["form"].validate((valid) => {
                if (valid) {
                    if (this.isStatus) {
                        editList(this.form).then((response) => {
                            this.$modal.msgSuccess("修改成功");
                            this.cancel()
                            this.getDataList();
                        });
                    } else {
                        addList(this.form).then((response) => {
                            this.$modal.msgSuccess("新增成功");
                            this.cancel()
                            this.getDataList();
                        });
                    }
                }
            });
        },
    },
}
</script>

<style lang="scss" scoped></style>